/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.fields;

import com.daikit.commons.shared.utils.DkIdGenerator;
import com.daikit.daikit4gxt.client.log.BaseLogger;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.BorderStyle;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.DOM;
import com.sencha.gxt.widget.core.client.event.TriggerClickEvent;
import com.sencha.gxt.widget.core.client.event.TriggerClickEvent.HasTriggerClickHandlers;
import com.sencha.gxt.widget.core.client.event.TriggerClickEvent.TriggerClickHandler;
import com.sencha.gxt.widget.core.client.form.TriggerField;


public abstract class ImageField extends TriggerField<String> implements HasTriggerClickHandlers
{

	protected BaseLogger log = BaseLogger.getLog(getClass());

	public static final String IMAGE_FIELD_CLASSNAME = "img-field";
	public static final String THUMB_ELEMENT_CLASSNAME = "thumb";

	public static final int DEFAULT_IMAGE_WIDTH = 80;
	public static final int DEFAULT_IMAGE_HEIGHT = 80;

	private Integer imageWidth = null;
	private Integer imageHeight = null;

	private final Element imageElement;

	private final String thumbWrapElementId;

	public ImageField()
	{
		super(new ImageCell());
		this.thumbWrapElementId = DkIdGenerator.generateRandomId(8);
		setElement(DOM.createDiv());
		getElement().setAttribute("role", "presentation");
		final Element divThumbContainerElement = DOM.createDiv();
		final Element divThumbElement = DOM.createDiv();
		imageElement = DOM.createImg();
		getElement().appendChild(divThumbContainerElement);
		divThumbContainerElement.appendChild(divThumbElement);
		divThumbElement.appendChild(imageElement);
		divThumbContainerElement.addClassName(IMAGE_FIELD_CLASSNAME);
		divThumbContainerElement.getStyle().setBorderWidth(1, Unit.PX);
		divThumbContainerElement.getStyle().setBorderStyle(BorderStyle.SOLID);
		divThumbContainerElement.getStyle().setBorderColor("white");
		divThumbElement.addClassName(THUMB_ELEMENT_CLASSNAME);

		addValueChangeHandler(new ValueChangeHandler<String>()
		{
			@Override
			public void onValueChange(final ValueChangeEvent<String> paramValueChangeEvent)
			{
				log.debug("Change value event received. Field value=" + getValue());
				updateImageSrc();
			}
		});
		addTriggerClickHandler(new TriggerClickHandler()
		{
			@Override
			public void onTriggerClick(final TriggerClickEvent event)
			{
				log.debug("On trigger click. Field value=" + getValue());
				onImageClick();
			}
		});
	}

	@Override
	public ImageCell getCell()
	{
		return (ImageCell) super.getCell();
	}

	protected Element getImageEl()
	{
		return getInputEl();
	}

	protected void initialize()
	{
		// nothing done by default
	}

	protected abstract String getEmptyImageSrc();

	protected abstract String getEmptyImageTitle();

	protected abstract String getImageSrc(String imageId);

	protected abstract String getImageTitle();

	protected abstract void onImageClick();

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// GETTERS / SETTERS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	protected void updateImageSrc()
	{
		final String imageId = getValue();
		getImageEl().setAttribute("src", imageId != null ? getImageSrc(imageId) : getEmptyImageSrc());
		addStyleName("x-form-field-wrap"); // TODO voir si ça met pas la classe plusieurs fois.
		getImageEl().setId(getId() + "-img");
		if (getImageHeight() != null)
		{
			getImageEl().setAttribute("height", getImageHeight() + "px");
		}
		if (getImageWidth() != null)
		{
			getImageEl().setAttribute("width", getImageWidth() + "px");
		}
		getImageEl().setAttribute("title", getImageTitle() != null ? getImageTitle() : getEmptyImageTitle());
	}

	public String getThumbWrapElementId()
	{
		return thumbWrapElementId;
	}

	public Integer getImageWidth()
	{
		return imageWidth;
	}

	public void setImageWidth(final Integer imageWidth)
	{
		this.imageWidth = imageWidth;
	}

	public Integer getImageHeight()
	{
		return imageHeight;
	}

	public void setImageHeight(final Integer imageHeight)
	{
		this.imageHeight = imageHeight;
	}
}
